package cn.edu.hit.core.db;

import cn.edu.hit.core.Document;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class DBHelper extends DBReadHelper {
    private final DataWriter writer;

    public DBHelper(DataWriter writer) {
        super(writer);
        this.writer = writer;
    }

    public boolean removeConfigure(String _id) {
        return writer.deleteById(_id);
    }

    public List<Map> persistConfigures(String name, String type, List<Map> configure) {
        return configure.stream().map(conf -> {
            conf.put("name", name);
            conf.put("type", type);
            return writer.saveOrUpdate(new Document(conf));
        }).collect(Collectors.toList());
    }

    public List<Map> persistMessage(List<Map> results) {
        return results.stream()
                .map(conf -> writer.saveOrUpdate(new Document(conf)))
                .collect(Collectors.toList());
    }

    public Document persistMessage(Map result) {
        return writer.saveOrUpdate(new Document(result));
    }
}
